Finish Ethereum and Dummy adapter#143
Conversation
2fe1017 to
cd96bae
Compare
66f7e89 to
f5f806a
Compare
adapter/contract/AdExCore.json
Outdated
| @@ -0,0 +1 @@ | |||
| [{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"withdrawn","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"},{"name":"","type":"address"}],"name":"withdrawnPerUser","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"components":[{"name":"creator","type":"address"},{"name":"tokenAddr","type":"address"},{"name":"tokenAmount","type":"uint256"},{"name":"validUntil","type":"uint256"},{"name":"validators","type":"address[]"},{"name":"spec","type":"bytes32"}],"name":"channel","type":"tuple"}],"name":"channelOpen","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"components":[{"name":"creator","type":"address"},{"name":"tokenAddr","type":"address"},{"name":"tokenAmount","type":"uint256"},{"name":"validUntil","type":"uint256"},{"name":"validators","type":"address[]"},{"name":"spec","type":"bytes32"}],"name":"channel","type":"tuple"},{"name":"stateRoot","type":"bytes32"},{"name":"signatures","type":"bytes32[3][]"},{"name":"proof","type":"bytes32[]"},{"name":"amountInTree","type":"uint256"}],"name":"channelWithdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"components":[{"name":"creator","type":"address"},{"name":"tokenAddr","type":"address"},{"name":"tokenAmount","type":"uint256"},{"name":"validUntil","type":"uint256"},{"name":"validators","type":"address[]"},{"name":"spec","type":"bytes32"}],"name":"channel","type":"tuple"}],"name":"channelWithdrawExpired","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"states","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"channelId","type":"bytes32"}],"name":"LogChannelOpen","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"channelId","type":"bytes32"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogChannelWithdrawExpired","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"channelId","type":"bytes32"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogChannelWithdraw","type":"event"}] No newline at end of file | |||
There was a problem hiding this comment.
can't you include adex-protocol-eth as a submodule?
There was a problem hiding this comment.
I have included it
adapter/src/dummy.rs
Outdated
| @@ -1,7 +1,7 @@ | |||
| #![deny(clippy::all)] | |||
| #![deny(rust_2018_idioms)] | |||
There was a problem hiding this comment.
rust-lang/rust#52047
Some good lints for the 2018 edition
adapter/src/dummy.rs
Outdated
| // @TODO | ||
| Ok("hello".to_string()) | ||
| fn session_from_token(&mut self, token: &str) -> AdapterResult<Session> { | ||
| let mut identity = ""; |
There was a problem hiding this comment.
you should use a functional style here
let identity = self.tokens_for_auth.iter().find(|(key, val)| val == token)
adapter/src/dummy.rs
Outdated
|
|
||
| match who { | ||
| Some((id, _)) => { | ||
| let auth = self.tokens_for_auth.get(&id).unwrap(); |
There was a problem hiding this comment.
this unwrap should be expect so that you can explain why you make that assumption
adapter/src/ethereum.rs
Outdated
| None, | ||
| &Some(self.keystore_pwd.clone()), | ||
| ) | ||
| .expect("Failed to create account"); |
There was a problem hiding this comment.
why doesn't this just return an Err?
adapter/src/ethereum.rs
Outdated
| public_to_address( | ||
| &wallet | ||
| .public(&self.keystore_pwd) | ||
| .expect("failed to get public key") |
There was a problem hiding this comment.
same...err handling should use Err
adapter/src/ethereum.rs
Outdated
| contract_address, | ||
| include_bytes!("../contract/Identity.json"), | ||
| ) | ||
| .unwrap(); |
There was a problem hiding this comment.
this is OK to be unwrapped, but you should use lazy_static for this
adapter/src/ethereum.rs
Outdated
|
|
||
| let contract_query = | ||
| contract.query("privileges", verified.from, None, Options::default(), None); | ||
| let priviledge_level: U256 = contract_query.wait().unwrap(); |
There was a problem hiding this comment.
let's keep this like that for now
but we should change this to an async call ASAP
adapter/src/ethereum.rs
Outdated
| (None, Some(wallet)) => { | ||
| let payload = Payload { | ||
| id: validator.id.clone(), | ||
| era: usize::try_from(Utc::now().timestamp()).expect("failed to parse utc now"), |
There was a problem hiding this comment.
this is an actual OK case for expect! cause there's no realistic way it can fail
| ); | ||
|
|
||
| // Sign | ||
| let expected_response = |
There was a problem hiding this comment.
those match with the JS impl, right?
f5f806a to
6c1a330
Compare
8fcc1b2 to
6a824f3
Compare
7aaa0c5 to
11b1355
Compare
11b1355 to
cb14290
Compare
92d1536 to
5337baf
Compare
2c09fce to
8fb4304
Compare
| pub token_addr: String, | ||
| pub token_amount: String, | ||
| pub valid_until: String, | ||
| pub valid_until: i64, |
There was a problem hiding this comment.
is this milliseconds (can we document this)? Should we try to use DateTime?
There was a problem hiding this comment.
i64 works fine but the param to new requires Datetime
There was a problem hiding this comment.
Yeah, I mean it's more about documenting it, creating the EthereumChannel requires Datetime, but when you use the public field valid_until from an object, you don't know where it comes from and what it is.
ad941aa to
d86a7e8
Compare
d86a7e8 to
e4739e4
Compare
|
@elpiel The suggested changes have been made |
| let whoami = adapter.whoami(); | ||
|
|
||
| let validator = self | ||
| .channel | ||
| .spec | ||
| .validators | ||
| .into_iter() | ||
| .find(|&v| v.id == whoami); | ||
| let auth_token = self | ||
| .adapter | ||
| .get_auth(validator.unwrap()) | ||
| .expect("Failed to get user auth token"); | ||
|
|
||
| let auth_token = adapter.get_auth(validator.unwrap()); |
There was a problem hiding this comment.
Even though we have fairly small amount of code, I see the confusion that leads to duplicating code by accident.
Here is what I don't understand: Why do we even bother to do this check, if we already have it in the initialization of the SentryInterface?
Why search for the adapter.whoami() in the channle.spec.validators and then even validator.unwrap() the value?
This whole thing can be simplified to just:
let auth_token = adapter.get_auth(adapter.whoami());
If you follow the implementation, the adapter.whoami() will always be included in the Channel validators, but it's just not obvious.
There was a problem hiding this comment.
adapter.whoami() returns a string,
.find(|&v| v.id == whoami); returns a ValidatorDesc
There was a problem hiding this comment.
Also, the idea behind the unwrap() is because we already check it in new() so it would always be there
| .expect("Failed to get user auth token"); | ||
|
|
||
| let auth_token = adapter.get_auth(validator.unwrap()); | ||
| if let Err(e) = auth_token { |
There was a problem hiding this comment.
You could map this error and use ? and get rid off the unwrap() bellow.
Same way you did here: https://github.com/AdExNetwork/adex-validator-stack-rust/pull/143/files/e4739e44f81d4c040f627043aa33bbb160c21a71..a781eb41a68ffa5cd31c3b42bfb2de638f773d29#diff-ae6ae3e6a4852afdf6f5eda167faccd7R189
a781eb4 to
4e50236
Compare
0a58df2 to
cf785d4
Compare
cf785d4 to
8be6f50
Compare
#142